﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace TeamResourceManageAssist
{
	public partial class GantSyleBar : UserControl
	{
		public static DependencyProperty StartDateProperty ;

        public static DependencyProperty EndDateProperty;

        public static DependencyProperty ChartStartDateProperty;

        public static DependencyProperty ChartEndDateProperty;
		
		public static DependencyProperty BarColorProperty;

        public static DependencyProperty TestProperty;

        static GantSyleBar()
        {
            TestProperty = DependencyProperty.Register("Test", typeof(int), typeof(GantSyleBar), new PropertyMetadata(1));

            StartDateProperty = DependencyProperty.Register("StartDate",
                typeof(DateTime), typeof(GantSyleBar),
                new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnStartDatePropertyChanged)));

            EndDateProperty = DependencyProperty.Register("EndDate",
                typeof(DateTime), typeof(GantSyleBar),
                new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnEndDatePropertyChanged)));

            ChartStartDateProperty = DependencyProperty.Register("ChartStartDate",
                typeof(DateTime), typeof(GantSyleBar),
                new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnChartStartDatePropertyChanged)));

            ChartEndDateProperty = DependencyProperty.Register("ChartEndDate",
                typeof(DateTime), typeof(GantSyleBar),
                new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnChartEndDatePropertyChanged)));

            BarColorProperty = DependencyProperty.Register("BarColor",
                typeof(Color), typeof(GantSyleBar),
                new PropertyMetadata(Color.FromArgb(255, 0, 0, 255), 
                    new PropertyChangedCallback(OnBarColorPropertyChanged)));
        }

        static void OnStartDatePropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            GantSyleBar dp = target as GantSyleBar;
            dp.StartDate = (DateTime)e.NewValue;
            dp.RefreshCanvas();
        }

        static void OnEndDatePropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            GantSyleBar dp = target as GantSyleBar;
            dp.EndDate = (DateTime)e.NewValue;
            dp.RefreshCanvas();
        }

        static void OnChartStartDatePropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            GantSyleBar dp = target as GantSyleBar;
            dp.ChartStartDate = (DateTime)e.NewValue;
            dp.RefreshCanvas();
        }

        static void OnChartEndDatePropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            GantSyleBar dp = target as GantSyleBar;
            dp.ChartEndDate = (DateTime)e.NewValue;
            dp.RefreshCanvas();
        }

        static void OnBarColorPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            GantSyleBar dp = target as GantSyleBar;
            dp.GradientStop1.Color = (Color)e.NewValue;
            dp.TimeBar.Stroke = new SolidColorBrush((System.Windows.Media.Color)e.NewValue);
        }

        public int Test
        {
            get { return (int)GetValue(TestProperty); }
            set
            {
                SetValue(TestProperty, value);
                RefreshCanvas();
            }
        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime StartDate
        {
            get { return (DateTime)GetValue(StartDateProperty); }
            set { 
                SetValue(StartDateProperty, value);
                RefreshCanvas();
            }
        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime EndDate
        {
            get { return (DateTime)GetValue(EndDateProperty); }
            set { 
                SetValue(EndDateProperty, value);
                //RefreshCanvas();
            }
        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime ChartStartDate
        {
            get { return (DateTime)GetValue(ChartStartDateProperty); }
            set { 
                SetValue(ChartStartDateProperty, value);
                RefreshCanvas();
            }
        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime ChartEndDate
        {
            get { return (DateTime)GetValue(ChartEndDateProperty); }
            set { 
                SetValue(ChartEndDateProperty, value);
                RefreshCanvas();
            }
        }

        public Color BarColor
        {
            get { return (Color)GetValue(BarColorProperty); }
            set { SetValue(BarColorProperty, value); 
                //BarGradEndColor.Color = value;
            }
        }


		public GantSyleBar()
		{
			// Required to initialize variables
			InitializeComponent();
		}
		
		public void RefreshCanvas()
		{
            Width = (ChartEndDate.Subtract(ChartStartDate).Days + 1) * 20;
            BackGroundPanel.Children.Clear();
            int n = ChartEndDate.Subtract(ChartStartDate).Days / 7 + 1;
            for (int i = 0; i < n; i++)
            {
                WeekColor weekColor = new WeekColor();
                weekColor.HorizontalAlignment = HorizontalAlignment.Left;
                BackGroundPanel.Children.Add(weekColor);
            }
            TimeBar.Margin = new Thickness(StartDate.Subtract(ChartStartDate).Days * 20, 
               3,  
               ChartEndDate.Subtract(EndDate).Days * 20, 1);
		}
	}
}